package offer.diff;

/**
 * @author DengYuan2
 * @create 2021-03-22 18:53
 */
public class n_43 {
    public static void main(String[] args) {
        n_43 n = new n_43();
        int res = n.NumberOf1Between1AndN_Solution(130);
        System.out.println(res);
    }

    /**
     * 思路：https://leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/solution/mian-shi-ti-43-1n-zheng-shu-zhong-1-chu-xian-de-2/
     * @param n
     * @return
     */
    public int NumberOf1Between1AndN_Solution(int n) {
        int digit=1,res=0;
        int high=n/10;
        int cur=n%10;
        int low = 0;
        while (high!=0 || cur!=0){
            if (cur==0){
                res+=high*digit;
            }else if (cur==1){
                res+=high*digit+low+1;
            }else {
                res+=(high+1)*digit;
            }
            low+=cur*digit;
            cur=high%10;
            high/=10;
            digit*=10;
        }
        return res;
    }
}
